package org.jeecg.modules.fund.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.fund.entity.UserhomeFund;
import org.jeecg.modules.fund.entity.UserhomeOrder;
import org.jeecg.modules.fund.service.IUserhomeFundService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 用户基金
 * @Author: jeecg-boot
 * @Date:   2024-08-29
 * @Version: V1.0
 */
@Api(tags="用户基金")
@RestController
@RequestMapping("/fund/userhomeFund")
@Slf4j
public class UserhomeFundController extends JeecgController<UserhomeFund, IUserhomeFundService> {
	@Autowired
	private IUserhomeFundService userhomeFundService;
	
	/**
	 * 分页列表查询
	 *
	 * @param userhomeFund
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "用户基金-分页列表查询")
	@ApiOperation(value="用户基金-分页列表查询", notes="用户基金-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<UserhomeFund>> queryPageList(UserhomeFund userhomeFund,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		UserhomeFund temp=new UserhomeFund();
		if(oConvertUtils.isNotEmpty(userhomeFund.getStatus()))
		{
			temp.setStatus(userhomeFund.getStatus());
			userhomeFund.setStatus(null);
		}
		QueryWrapper<UserhomeFund> queryWrapper = QueryGenerator.initQueryWrapper(userhomeFund, req.getParameterMap());
		Page<UserhomeFund> page = new Page<UserhomeFund>(pageNo, pageSize);
		if(oConvertUtils.isNotEmpty(userhomeFund.getFundname()))
		{
			queryWrapper.like("g.name",userhomeFund.getFundname());
		}
		if(oConvertUtils.isNotEmpty(userhomeFund.getFcode()))
		{
			queryWrapper.eq("g.fcode",userhomeFund.getFcode());
		}
		if(oConvertUtils.isNotEmpty(userhomeFund.getPhone()))
		{
			queryWrapper.like("u.phone",userhomeFund.getPhone());
		}
		if(oConvertUtils.isNotEmpty(temp.getStatus()))
		{
			queryWrapper.eq("a.status",temp.getStatus());
		}
		IPage<UserhomeFund> pageList = userhomeFundService.pageList(page, queryWrapper);
		return Result.OK(pageList);
	}





	 /**
	  *
	  *
	  * @param jsonObject
	  * @return
	  */
	 @AutoLog(value = "基金-禁止恢复操作")
	 @ApiOperation(value="商品-禁止恢复操作", notes="商品-禁止恢复操作")
	 //@RequiresPermissions("org.jeecg.modules:wisdom_person:deleteBatch")
	 @RequestMapping(value = "/shelves", method = {RequestMethod.PUT})
	 public Result<String> shelves(@RequestBody JSONObject jsonObject) {
		 String ids = jsonObject.getString("ids");
		 Integer status = jsonObject.getInteger("status");
		 List<String> listArray=Arrays.asList(ids.split(","));
		 for (String temp:listArray)
		 {
			 UserhomeFund userhomeFund=userhomeFundService.getById(temp);
			 userhomeFund.setStatus(status);
			 if(userhomeFund.getStatus()==3)
			 {
				 continue;
			 }
			 if(status==1)
			 {
				 if(userhomeFund.getRedeemNum()!=0)
				 {
					 userhomeFund.setStatus(2);
				 }
			 }
			 userhomeFundService.updateById(userhomeFund);
		 }
		 return Result.OK("批量操作成功!");
	 }
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "用户基金-通过id查询")
	@ApiOperation(value="用户基金-通过id查询", notes="用户基金-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<UserhomeFund> queryById(@RequestParam(name="id",required=true) String id) {
		UserhomeFund userhomeFund = userhomeFundService.getById(id);
		if(userhomeFund==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(userhomeFund);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param userhomeFund
    */
    //@RequiresPermissions("org.jeecg.modules:userhome_fund:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, UserhomeFund userhomeFund) {
        return super.exportXls(request, userhomeFund, UserhomeFund.class, "用户基金");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("userhome_fund:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, UserhomeFund.class);
    }

}
